/*
 * HTTPConnection.java
 *
 * Created on 3 de Abril de 2006, 19:42
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package org.mobiletools.fuelmanager.connection;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;

/**
 *
 * @author instrutor
 */
public class HTTPConnection {
    private HTTPResponseListener rl;
    public HTTPConnection(HTTPResponseListener rl){
        this.rl = rl;
    }
   
    public void sendRequest(String requestURL, String requestData){
        Sender s = new Sender(requestURL,requestData);
        s.start();
   }
    
    
    private class Sender extends Thread {
        
        private String URL;
        private String data;
        
        public Sender(String URL, String data){
            this.URL = URL;
            this.data =data;
        }
        
        public void run(){
                HttpConnection connection = null;
		OutputStream os = null;
                InputStream is = null;
		try {
			connection = (HttpConnection) Connector.open(URL+"?"+this.data);
			connection.setRequestMethod("GET");
                        connection.setRequestProperty("Content-Type","text/xml");
			/*os = connection.openOutputStream();
                        System.out.println(this.data);
                        OutputStreamWriter sw = new OutputStreamWriter(os, "UTF-8");
                        sw.write(this.data);
                        sw.flush();
                        sw.close();
			*/
			int code = connection.getResponseCode();
			String responseMessage = connection.getResponseMessage();
                        if (code!=HttpConnection.HTTP_OK) {
				rl.response(code,responseMessage,null);
			}
			is = connection.openInputStream();
                        String returnData = readData(is);
                        rl.response(HTTPResponseListener.DATA_RECEIVED,responseMessage,returnData);
		}catch(Exception e) {
			rl.response(-1,e.getMessage(),null);
		}finally{
                    try {
                        if (os != null) {
                            os.close();
                        }
                        if (is != null) {
                            is.close();
			}
                        if (connection != null) {
                                connection.close();
                        }
                    } catch (Exception e) {
                            rl.response(-1,e.getMessage(),null);
                    }
		}
        }
        
        private String readData(InputStream is) throws IOException{
            InputStreamReader isr = new InputStreamReader(is, "UTF-8");
            int c;
            String str = new String();
            while ((c = isr.read()) != -1) {
                    str += (char) c;
            }
            System.out.println("Received Buffer:"+str);
            return str;
        }	
    }
    
}
